﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ERP.Common.Results
{
    /// <summary>
    /// API响应包装类
    /// </summary>
    public class APIResult<T>
    {
        /// <summary>
        /// 返回的数据
        /// </summary>
        public T Date { get; set; }
        
        /// <summary>
        /// 返回的数据（兼容性属性）
        /// </summary>
        public T Data 
        { 
            get => Date; 
            set => Date = value; 
        }
        
        /// <summary>
        /// 操作是否成功
        /// </summary>
        public bool Success { get; set; }
        
        /// <summary>
        /// 操作是否成功（兼容性属性）
        /// </summary>
        public bool IsSuccess 
        { 
            get => Success; 
            set => Success = value; 
        }
        
        /// <summary>
        /// 返回的消息提示
        /// </summary>
        public string Message { get; set; } = string.Empty;
        
        /// <summary>
        /// 返回的状态码
        /// </summary>
        public EnumResult Code { get; set; }
        
        /// <summary>
        /// 创建成功结果
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="message">消息</param>
        /// <returns>API结果</returns>
        public static APIResult<T> CreateSuccess(T data, string message = "操作成功")
        {
            return new APIResult<T>
            {
                Success = true,
                Date = data,
                Message = message,
                Code = EnumResult.成功
            };
        }
        

        
        /// <summary>
        /// 创建失败结果
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns>API结果</returns>
        public static APIResult<T> CreateError(string message = "操作失败")
        {
            return new APIResult<T>
            {
                Success = false,
                Date = default(T),
                Message = message,
                Code = EnumResult.系统异常
            };
        }
        
        /// <summary>
        /// 创建失败结果（简化版本）
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <returns>API结果</returns>
        public static APIResult<T> Failed(string message = "操作失败")
        {
            return CreateError(message);
        }
    }
}
